﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlueLotus7.ArchiveSystem.Common.Contracts.Sina.Weibo;
using System.Xml;
using System.Xml.Linq;

namespace BlueLotus7.ArchiveSystem.SinaContentApi.Weibo
{
    partial class ContentAnalyzer
    {
        #region User Info in Home page.
        //  <div class="u">
        //    <table>
        //        <tr>
        //            <td valign="top">
        //                <a href="/1783906952/info?vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">
        //                    <img src="http://tp1.sinaimg.cn/1783906952/50/5609603970/0" alt="头像" class="por" /></a>
        //            </td>
        //            <td valign="top">
        //                <div class="ut">
        //                    末月de鱼<a href="/dpool/ttt/chunjie.php?fr=icon&amp;vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261"><img
        //                        src="http://u1.sinaimg.cn/upload/2012/01/06/egg0.gif" alt="龙蛋" /></a>/女/上海&nbsp;<a
        //                            href="/attention/add/1783906952?rl=0&amp;vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">关注她</a><br />
        //                    典型的射手座<br />
        //                    <a href="/msg/chat/send?uid=1783906952&amp;nick=末月de鱼&amp;rl=0&amp;vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">
        //                        私信</a>&nbsp;<a href="/1783906952/info?rl=0&amp;vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">资料及操作</a>&nbsp;<a
        //                            href="/attgroup/special?fuid=1783906952&amp;vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">特别关注</a></div>
        //            </td>
        //        </tr>
        //    </table>
        //    <div class="tip2">
        //        <span class="tc">微博[1814]</span>&nbsp;<a href="/1783906952/follow?vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">关注[86]</a>&nbsp;<a
        //            href="/1783906952/fans?vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">粉丝[138]</a>&nbsp;<a
        //                href="/at/weibo?uid=1783906952&amp;vt=4&amp;gsid=3_5bc353f8e36c2c83509a65bbeb0af98e0a435261">@她的</a></div>
        //</div>

        public static List<WeiboArticle> TranslateWeiboUserAndArticlesInHomePage(string htmlContent, out string userIdentity, bool translateUser, bool translateArticle)
        {
            List<WeiboArticle> articleContainer = new List<WeiboArticle>();
            userIdentity = null;

            if (!string.IsNullOrWhiteSpace(htmlContent))
            {
                htmlContent = CleanHtmlContent(htmlContent);
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(htmlContent);

                articleContainer = TranslateWeiboUserAndArticlesInHomePage(xmlDocument.DocumentElement, out userIdentity, translateUser, translateArticle);
            }

            return articleContainer;
        }

        private static List<WeiboArticle> TranslateWeiboUserAndArticlesInHomePage(XmlNode bodyXml, out string userIdentity, bool translateUser, bool translateArticle)
        {
            List<WeiboArticle> articleContainer = new List<WeiboArticle>();
            userIdentity = null;

            if (bodyXml != null && bodyXml.Name == "body")
            {
                XmlNode userNode = bodyXml.SelectSingleNode("descendant::div[@class='u']");
                WeiboUser user = TranslateWeiboUserForHomeSummary(userNode);
                userIdentity = user.ThirdPartyIdentity;
            }

            return articleContainer;
        }

        public static WeiboUser TranslateWeiboUserForHomeSummary(string htmlContent)
        {
            WeiboUser user = null;

            if (!string.IsNullOrWhiteSpace(htmlContent))
            {
                htmlContent = CleanHtmlContent(htmlContent);
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(htmlContent);

                user = TranslateWeiboUserForHomeSummary(xmlDocument.DocumentElement);
            }

            return user;
        }

        private static WeiboUser TranslateWeiboUserForHomeSummary(XmlNode uDivXml)
        {
            WeiboUser user = null;

            if (uDivXml != null && uDivXml.Name == "div" && GetXmlAttributeValue(uDivXml, "class") == "u")
            {
                user = new WeiboUser();

                //Get avator url
                XmlNode imageTag = uDivXml.SelectSingleNode("descendant::table/tr/td/a/img");
                user.AvatorUrl = GetXmlAttributeValue(imageTag, tag_src);

                //Fill avator data
                Uri avatorUri = new Uri(user.AvatorUrl);
                user.AvatorData = LoadBinary(avatorUri);



                //Get follow and fans
                XmlNode tip2 = uDivXml.SelectSingleNode("descendant::div[@class='tip2']");
            }

            return user;
        }

        #endregion

        #region User Info in User Info page

        //<body>
        //    <div class="c">
        //        <a href="/dpool/ttt/index.php?p=top" id="top" class="tl">广场</a>.<a href="/dpool/ttt/group.php?p=top"
        //            class="tl">微群</a>.<a href="/" class="tl">我的首页</a>.<a href="http://weibo.cn/1783906952/info?rl=0&amp;rand=6323&amp;p=r"
        //                class="tl">刷新</a>&nbsp;<a href="/2554870014/fans" class="kt">新增粉丝1人</a></div>
        //    <div class="n">
        //        <a href="/mblog/sendmblog?composer=1" class="nl">发微博</a>|<a href="/msg/" class="nl">消息</a>|<a
        //            href="http://weibo.cn/dpool/ttt/hotword.php?p=top" class="nl">话题</a>|<a href="/search/"
        //                class="nl">搜索</a>|<a href="/account/" class="nl">设置</a></div>
        //    <div class="ps">
        //        <a href="/dpool/ttt/help.php?cat=notice&amp;id=8207&amp;pos=322&amp;rl=1">除夕夜打麻将还是看春晚</a>&nbsp;<a
        //            href="/dpool/ttt/help.php?cat=notice&amp;id=8207&amp;cid=4&amp;backurl=http://weibo.cn/1783906952/info?rl=0&amp;rand=6323&amp;rl=1"><img
        //                src="http://u1.sinaimg.cn/upload/2011/08/01/5366.gif" alt="[X]" /></a></div>
        //    <div class="c">
        //        <a href="/?rand=419033">返回我的首页</a></div>
        //    <div class="c">
        //        <a href="/msg/chat/send/?nick=末月de鱼&amp;uid=1783906952&amp;rl=1">私信</a>&nbsp;<a href="/dpool/ttt/invite.php?act=show&amp;uid=1783906952&amp;rl=1">推荐</a>&nbsp;<a
        //            href="/attention/addb/1783906952?rl=1">屏蔽</a>&nbsp;<a href="/attention/add/1783906952?rl=1">关注她</a>&nbsp;</div>
        //    <div class="c">
        //        <img src="http://tp1.sinaimg.cn/1783906952/180/5609603970/0" alt="头像" /></div>
        //    <div class="c">
        //        <img src="http://img.t.sinajs.cn/t4/style/images/medal/290_s.gif?version=201112261811"
        //            alt="名震江湖" />&nbsp;<img src="http://img.t.sinajs.cn/t4/style/images/medal/370_s.gif?version=201112261811"
        //                alt="意气相投" />&nbsp;<img src="http://img.t.sinajs.cn/t4/style/images/medal/107_s.gif?version=201112261811"
        //                    alt="斗酒百篇" />&nbsp;<img src="http://img.t.sinajs.cn/t4/style/images/medal/417_s.gif?version=201112261811"
        //                        alt="灵感潮人" />&nbsp;<img src="http://img.t.sinajs.cn/t4/style/images/medal/414_s.gif?version=201112261811"
        //                            alt="微博之力" />&nbsp;<a href="/dpool/ttt/medalnew.php?uid=1783906952">更多勋章</a></div>
        //    <div class="tip">
        //        基本信息</div>
        //    <div class="c">
        //        昵称:末月de鱼<br />
        //        性别:女<br />
        //        地区:上海 闸北区<br />
        //        生日:1986-12-01<br />
        //        简介:典型的射手座<br />
        //        标签:<a href="/search/?keyword=%E5%B7%A7%E5%85%8B%E5%8A%9B%E7%8B%82%E4%BA%BA&amp;stag=1">巧克力狂人</a>&nbsp;<a
        //            href="/search/?keyword=%E5%A4%9C%E8%A1%8C%E5%8A%A8%E7%89%A9&amp;stag=1">夜行动物</a>&nbsp;<a
        //                href="/search/?keyword=%E8%8A%9D%E5%A3%AB%E6%8E%A7&amp;stag=1">芝士控</a>&nbsp;<a href="/account/privacy/tags/?uid=1783906952">更多&gt;&gt;</a><br />
        //    </div>
        //    <div class="tip">
        //        学习经历</div>
        //    <div class="c">
        //        ·上海外国语大学<br />
        //        ·上海外国语大学&nbsp;04级<br />
        //        ·Curtin University of Technology&nbsp;08级<br />
        //    </div>
        //    <div class="tip">
        //        其他信息</div>
        //    <div class="c">
        //        互联网:http://weibo.com/333861201<br />
        //        手机版:http://weibo.cn/333861201<br />
        //        <a href="/dpool/ttt/album.php?act=albumlist&amp;fuid=1783906952">她的相册&gt;&gt;</a></div>
        //    <div class="cd">
        //        <a href="#top">
        //            <img src="http://u1.sinaimg.cn/3g/image/upload/0/62/203/18979/5e990ec2.gif" alt="TOP" /></a></div>
        //    <div class="nfw">
        //        <a href="/news/">随便看看</a>.<a href="/guess/">猜你喜欢</a>.<a href="/dpool/ttt/votehome.php">投票</a>.<a
        //            href="/news/city">同城</a><br />
        //        <a href="/dpool/ttt/index.php">微博广场</a>.<a href="/">我的首页</a>.<a href="/dpool/ttt/topic.php?tid=452">反馈</a>.<a
        //            href="/dpool/ttt/page.php?id=91">帮助</a><br />
        //    </div>
        //    <div class="c">
        //        <a href="/dpool/ttt/smsfeed.php?act=index">短信</a>.<a href="/dpool/ttt/topic.php?tid=1026">微播</a>.<a
        //            href="http://wap.baoruan.com/weibo/default/index/return/1/soft_id/1/mid/0">客户端</a>.<a
        //                href="/spam/?rl=1" class="kt">举报</a>.<a href="http://3g.sina.com.cn/prog/wapsite/sso/loginout.php?backURL=http%3A%2F%2Fweibo.cn%2Fdpool%2Fttt%2Findex.php%3Fvt%3D&amp;backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&amp;vt=">退出</a></div>
        //    <div class="b">
        //        <a href="http://3g.sina.com.cn/3g/pro/index.php?sa=t271d6v147">加入我们</a>|<a href="http://3g.sina.com.cn/3g/pro/index.php?sa=t271d13v147">联系我们</a><br />
        //        <a href="http://sina.cn" class="bl">手机新浪网</a>-<a href="http://3g.sina.com.cn/3g/news/index.php?tid=124&amp;did=13098&amp;vid=84&amp;pos=101"
        //            class="bl">导航</a>-<a href="http://3g.sina.com.cn/iask/?p=3g&amp;pos=101" class="bl">搜索</a><br />
        //        Sina.cn[01-22 14:40]</div>
        //</body>

        public static WeiboUser TranslateWeiboUser(string htmlBody)
        {
            return null;
        }

        private static WeiboUser TranslateWeiboUser(XmlNode bodyXml)
        {
            WeiboUser user = null;

            if (bodyXml != null && bodyXml.Name == "body")
            {
                user = new WeiboUser();

                //Get avator url
                XmlNode imageTag = bodyXml.SelectSingleNode("descendant::div[@class='c']/img");
                user.AvatorUrl = GetXmlAttributeValue(imageTag, tag_src);

                //Fill avator data
                HandleAvator(user, user.AvatorUrl);

                //Get basic info
                XmlNode tipNode = bodyXml.SelectSingleNode("descendant::div[@class='tip']");
                XmlNode baseInfoNode = tipNode.SelectSingleNode("following::div[@class='c']");

            }

            return user;
        }

        #endregion



        private static void HandleAvator(WeiboUser user, string avatorUrl)
        {
            if (user != null && !string.IsNullOrWhiteSpace(avatorUrl))
            {
                Uri avatorUri = new Uri(user.AvatorUrl);
                user.AvatorData = LoadBinary(avatorUri);
            }
        }
    }
}
